package com.yunhe.sms.service.system.impl;

import com.yunhe.commens.dto.Pagedao;
import com.yunhe.sms.dao.system.PremissionDao;
import com.yunhe.sms.dao.system.impl.PremissionDaoImpl;
import com.yunhe.sms.dto.Menus;
import com.yunhe.sms.dto.TreeNode;
import com.yunhe.sms.entity.system.Premission;
import com.yunhe.sms.service.system.PremissionService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/19 0019 10:42
 * @Description: 实现接口
 */
public class PremissionServiceImpl  implements PremissionService {

    private PremissionDao premissionDao = new PremissionDaoImpl ();
    @Override
    public void save(Premission premission) {
        premissionDao.save ( premission );
    }

    @Override
    public void deleted(String ids) {
        if (null!=ids&& !ids.isEmpty ()){
            String[] split = ids.split ( "," );
            for (String id:split){
                premissionDao.deleted ( Long.valueOf ( id ) );
            }
        }
    }

    @Override
    public void update(Premission premission) {
        premissionDao.update ( premission );
    }

    @Override
    public List<Premission> findPremi(Long rolId,int type) {
        return premissionDao.findPremi ( rolId,type );
    }

    @Override
    public Premission find(Long id) {
        Premission premission = premissionDao.find ( id );
        return premission;
    }

    @Override
    public List<Premission> findList() {
        List<Premission> list = premissionDao.findList ();
        return list;
    }

    @Override
    public List<TreeNode> findTreeNodes(Long rolId) {
        List<TreeNode> list=new ArrayList<> ();
        List<Premission> listPremi = this.findList ();//获取所有的权限

        List<Long> roll = premissionDao.findRoll ( rolId ); // 从数据库查出该角色的所有权限Id集合 （roll）
        if (null != listPremi){
            for (Premission premi:listPremi){
                TreeNode treeNode = new TreeNode ( premi.getId (), premi.getParentsId (), premi.getName () );

                if (roll.contains ( premi.getId () )){//判断当前权限的id 是否在roll 中，如果在则 说明该权限被 该角色所拥有（checked = true）
                    treeNode.setChecked ( Boolean.TRUE );//选中该权限
                }
                list.add ( treeNode);
            }
        }
        return list;
    }

    @Override
    public List<Premission> findrollpremi(Long rolId) {
        List<Premission> premissions = premissionDao.findrollpremi ( rolId );
        return premissions;
    }

    @Override
    public List<Premission> findType(int type) {
        return premissionDao.findType ( type );
    }

    @Override
    public List<Premission> findParent(int parentId) {
        return premissionDao.findParent ( parentId );
    }

    @Override
    public List<Premission> findTypeList(String str) {
        return premissionDao.findTypeList ( str );
    }

    @Override
    public Pagedao<Premission> page(int offset, int size) {
        return premissionDao.page ( offset, size );
    }

    @Override
    public List<Premission> findButton(Long parent_id) {
        return premissionDao.findButton ( parent_id );
    }
}
